

$(function () {
    $("#meeting-detail .btn-close").click(function() {
        $MB.closeAndRestModal("meeting-detail");
    });
    $("#meeting-detail").find("#meeting-detail-join-attendee").click(function() {
        var detail = $('#meeting-detail');
        var meetingId = $(detail).attr('meeting-id');
        var userId = $(detail).attr('user-id');
        var username = $(detail).attr('username');
        joinMeeting(false, meetingId, userId, username);
    });
    $("#meeting-detail").find("#meeting-detail-join-moderator").click(function() {
        var detail = $('#meeting-detail');
        var meetingId = $(detail).attr('meeting-id');
        var userId = $(detail).attr('user-id');
        var username = $(detail).attr('username');
        joinMeeting(true, meetingId, userId, username);
    });

    $("#meeting-detail").find("#meeting-detail-end").click(function() {
        endMeeting();
    });

    $("#meeting-detail").find("#meeting-detail-reopen").click(function() {
        var detail = $('#meeting-detail');
        var meetingId = $(detail).attr('meeting-id');

        reopenMeeting(meetingId);
    });
    $("#meeting-detail").find("#meeting-detail-reschedule").click(function() {
        var detail = $('#meeting-detail');
        var meetingId = $(detail).attr('meeting-id');

        rescheduleMeeting(meetingId);
    });
});

function showMeetingDetail(meetingId) {
    $.get(ctx + "meeting/detail/" + meetingId, function(r) {
        if(r.code == 0) {
            var meeting = r.msg;
            $("#meeting-detail-name").empty();
            $("#meeting-detail-name").append(meeting.name);

            var detail = $('#meeting-detail');
            $(detail).attr('meeting-id', meeting.id);
            $(detail).attr('user-id', meeting.currentUserId);
            $(detail).attr('username', meeting.currentUsername);

            //Tpv zhongxiang.huang 20181226,add for wechart invite
            var wecharInvite = $('#meeting-wechart-invite');

            $(detail).attr("meeting-id", meetingId);

            var topic = $(detail).find("#meeting-detail-topic");
            topic.empty();
            topic.append(meeting.topic);

            var content = $(detail).find("#meeting-detail-content");
            //Tpv zhongxiang.huang 20181229 modify-----start
            var meetingContent = meeting.content;
            var re = new RegExp("\n", "g");// 匹配所有的\n，g表示全部global
            content.empty();
            content.html(meetingContent.replace(re, "<br/>")).show();// html中解析换行用<br/>
            //content.append(meeting.content);
            var meetingOriginator = $(detail).find("#meeting-detail-originator");
            meetingOriginator.empty();
            meetingOriginator.append(meeting.username);
            //Tpv zhongxiang.huang 20181229 modify-----end

            var meetingTime = $(detail).find("#meeting-detail-time");
            meetingTime.empty();
            meetingTime.append($MB.formatDate(meeting.start, meeting.end));

            var inviteId = $(detail).find("#meeting-detail-id");
            inviteId.empty();
            inviteId.append(meeting.meetingId);

            var inviteId = $(detail).find("#meeting-detail-id");
            inviteId.empty();
            inviteId.append(meeting.meetingId);

            var meetingPassword = $(detail).find("#meeting-detail-password");
            meetingPassword.empty();
            meetingPassword.append(meeting.password);

            var inviteUrl = $(detail).find("#meeting-detail-url");
            inviteUrl.empty();
            inviteUrl.append(meeting.url);
            //Tpv zhongxiang.huang 20181229 modify-----start
            inviteUrl.attr('href', meeting.url);
            //Tpv zhongxiang.huang 20181229 modify-----end

            var members = $(detail).find("#meeting-detail-members");
            members.empty();
            if(meeting.members.length > 0) {
                members.append(meeting.members[0].nick);
                for(var i=1; i<meeting.members.length; i++) {
                    members.append(',');
                    members.append(meeting.members[i].nick);
                }
            }

            if(meeting.isEnd){
                $(detail).find("#meeting-detail-reopen").show();
                $(detail).find("#meeting-detail-reschedule").show();

                $(detail).find("#meeting-detail-join-attendee").hide();
                $(detail).find("#meeting-detail-join-moderator").hide();
                $(detail).find("#meeting-detail-end").hide();
            }else{
                $(detail).find("#meeting-detail-reopen").hide();
                $(detail).find("#meeting-detail-reschedule").hide();

                $(detail).find("#meeting-detail-join-attendee").show();
                $(detail).find("#meeting-detail-join-moderator").show();
                $(detail).find("#meeting-detail-end").show();
            }
            //$('#copyVal').val(meeting.inviteUrl);
            $(detail).find(".copyBtn").attr('data-clipboard-text',meeting.inviteUrl);
            $(detail).find("#meeting-detail-qrcode").attr("src", "data:image/png;base64," + meeting.qrcode);
            //Tpv zhongxiang.huang 20181226,add for wechart invite
            //$(wecharInvite).find("#meeting-wechart-invite-qrcode").attr("src", "data:image/png;base64," + meeting.inviteQrcode);
            var clipboard = new ClipboardJS('.copyBtn', {
                container: document.getElementById('meeting-detail')
            });
            $(detail).on("shown.bs.modal", function() {
                 clipboard.on('success', function(e) {
                      $MB.n_success($MB.i18n('meeting.copy.successtxt'));
                 });
                 clipboard.on('error', function(e) {
                     $MB.n_danger($MB.i18n('meeting.copy.failtxt'));
                 });
            });
            $(detail).on('hide.bs.modal', function () {
               if(clipboard){
                  clipboard.destroy();
               }
            });
            $(detail).modal();
        } else {
            $MB.n_danger(r.msg);
        }
    });
}

/*******
 * Tpv zhongxiang.huang 20181226--add by zhongxiang.huang,meeting invite----start
 * *******/
function wechartInviteMetting(e){
    $MB.closeModal(e.attr('id'));
    var meetingId=e.attr('meeting-id');
    openInviteWechart(meetingId);
}

function emailInviteMetting(e){
    $MB.closeModal(e.attr('id'));
    // Add by ken.he on 20190117 ---start---
    var meetingId=e.attr('meeting-id');
    $MB.n_success($MB.i18n('email.send.already'));
    $.post(ctx + "smtp/sendmail", {"meetingId": meetingId, "type":2 }, function(r){
		console.log(r);
		if (r.code === 0) {
//            $MB.n_success($MB.i18n('email.send.success'));
        } else if(r.code === 1){
        	$MB.n_danger($MB.i18n('email.failtxt.configs.error'));
        } else if(r.code === 2){
        	$MB.n_danger($MB.i18n('email.failtxt.address.error'));
        } else if(r.code === -1){
        	$MB.n_danger($MB.i18n('email.failtxt.configs.off'));
        } else if(r.code === -2){
        	$MB.n_danger(r.msg);
        }else {
            $MB.n_danger(r.msg);
        }
	});
    // Add by ken.he on 20190117 ---end---
}
/*******
 * Tpv zhongxiang.huang 20181226--add by zhongxiang.huang,meeting invite----end
 * *******/

var handle = false;

function joinMeeting(moderator, meetingId, userId, username) {
    if(!handle) {
        handle = true;
    } else {
        return;
    }

    var data = {"moderator" : moderator};
    $.ajax({
        type : "post",
        url : "/meeting/join/" + meetingId,
        cache : false,
        async: false,
        data: data,
        success : function(r) {
            if(r.code == 0) {
                window.open(r.msg.url, '_blank').location;
            } else {
                if(r.code == 609) {//locked
                    $MB.closeModal('meeting-detail');
                    showMeetingLock(meetingId, 'user-' + userId, username);
                } else {
                    $MB.n_danger($MB.i18n(r.msg));
                }
            }
            handle = false;
        },
        error: function() {
            handle = false;
        }
    });
}

function endMeeting(){
    $.post(ctx + "meeting/end", { "meetingId": $("#meeting-detail").attr("meeting-id")}, function(r) {
         if (r.code === 0) {
             $MB.n_success(r.msg);
         } else {
             $MB.n_danger(r.msg);
         }
    });
}


function reopenMeeting(meetingId) {
    $.get(ctx + "meeting/reopen/" + meetingId, function(r) {
        $MB.closeModal('meeting-detail');

        if (r.code === 0) {
            var meeting = r.msg;

            $('#repeat-now').iCheck('check');
            $meetingEditForm = $("#meeting-add-form")
            $meetingEditForm.attr("action", "add");

            $meetingEditForm.attr("meeting-id", meeting.id);

            $meetingEditForm.find("input[name='topic']").val(meeting.topic);
            $meetingEditForm.find("textarea[name='content']").val(meeting.content);

            document.getElementById("meeting-room-" + meeting.meetingRoomId).selected = true;

            if(meeting.durationId >= 0) {
                document.getElementById("duration-" + meeting.durationId).selected = true;
            }

            $("input[name='meeting-password']").val(meeting.meetingPassword);
            $("input[name='moderator-password']").val(meeting.moderatorPassword);

            var members = meeting.members;
            var jstree = $("#meeting-add-users").jstree(true);
            for(var i=0; i<members.length; i++) {
                jstree.check_node(members[i].user_id);
            }

            $("input[name='meeting-id']").val(meeting.meetingId);

            updateSettings(meeting.setting);

            $('#meeting-add').modal();
        } else {
            $MB.n_danger($MB.i18n(r.msg));
        }
    });
}

function rescheduleMeeting(meetingId) {
    $.get(ctx + "meeting/reschedule/" + meetingId, function(r) {
        $MB.closeModal('meeting-detail');

        if (r.code === 0) {
            var meeting = r.msg;


            $meetingEditForm = $("#meeting-add-form")
            $meetingEditForm.attr("action", "add");

            $meetingEditForm.attr("meeting-id", meeting.id);

            $meetingEditForm.find("input[name='topic']").val(meeting.topic);
            $meetingEditForm.find("textarea[name='content']").val(meeting.content);

            document.getElementById("meeting-room-" + meeting.meetingRoomId).selected = true;

            if(meeting.durationId >= 0) {
                document.getElementById("duration-" + meeting.durationId).selected = true;
            }

            if(meeting.type == 0) {
                $('#repeat-now').iCheck('check');
            } else if(meeting.type == 3) {
                $('#repeat-fix').iCheck('check');
            } else if(meeting.type == 2) {
                $('#repeat-on').iCheck('check');

                var days = meeting.repeatDay.split(",");

                for(var i=0; i<days.length; i++) {
                    $("#repeat-day-" + days[i]).iCheck("check");
                }
            } else {
                $('#repeat-off').iCheck('check');
            }

            if(meeting.type != 0) {// tomorrow
                var start = new Date(meeting.start);
                var date = new Date();//获取当前时间
                date.setDate(date.getDate() + 1);

                $("#meeting-date").val(date.format("yyyy-mm-dd"));
                $("#meeting-date-link").val(date.format("yyyy-mm-dd"));
                $("#meeting-time").val(start.format("hh:ii"));
                $("#meeting-time-link").val(start.format("hh:ii"));
            }

            var members = meeting.members;
            var jstree = $("#meeting-add-users").jstree(true);
            for(var i=0; i<members.length; i++) {
                jstree.check_node(members[i].user_id);
            }

            updateSettings(meeting.setting);

            $('#meeting-add').modal();
        } else {
            $MB.n_danger($MB.i18n(r.msg));
        }
    });
}